package cn.rokhdelar.ams.business.biz.project.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.rokhdelar.ams.base.FlowableCommentBasePo;
import cn.rokhdelar.ams.business.FlowableInstanceKeyConstant;
import cn.rokhdelar.ams.business.biz.project.domain.po.BizProjectInfo;
import cn.rokhdelar.ams.business.biz.project.service.BizProjectInfoService;
import cn.rokhdelar.ams.business.biz.project.service.BizProjectInitAuditService;
import cn.rokhdelar.ams.management.domain.po.SysUser;
import cn.rokhdelar.ams.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 范书铭
 * @date 2025年07月21日 10:09
 */
@Service
@RequiredArgsConstructor

public class BizProjectInitAuditServiceImpl implements BizProjectInitAuditService {
    private final TaskService taskService;

    private final RuntimeService runtimeService;

    private final BizProjectInfoService projectInfoService;

    @Override
    public boolean startAudit(String projectCode) {
        //验证projectCode是否存在
        List<BizProjectInfo> bizProjectInfoList = projectInfoService.list(new LambdaQueryWrapper<BizProjectInfo>().eq(BizProjectInfo::getProjectCode, projectCode));
        Assert.isTrue(bizProjectInfoList.size() == 1, "项目不存在");
        // 启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(FlowableInstanceKeyConstant.InitProjectAudit, projectCode);
        // 更新项目状态
        projectInfoService.updateStatus(projectCode, 1);
        return true;
    }

    @Override
    public boolean baseInfoAudit(FlowableCommentBasePo flowableCommentBasePo) {
        SysUser sysUser = SecurityUtils.getLoginUser();
        Map<String, Object> commited = flowableCommentBasePo.getVariables();
        // 1. 验证提交参数是否完整
        Assert.notNull( commited.get("access"), "审核结果不能为空");
        Integer reEdit  = (Integer) flowableCommentBasePo.getVariables().get("reEdit");
        // 1. 构建审核结果变量
        Map<String, Object> variables = new HashMap<>();
        variables.put("access ", reEdit); // 审核结果（通过/驳回）
        if(commited.containsKey("auditComment"))
        variables.put("auditComment", commited.get("auditComment")); // 审核意见
        //记录审核人信息
        taskService.setVariableLocal(flowableCommentBasePo.getTaskId(), "auditor", sysUser.getId());
        taskService.setVariableLocal(flowableCommentBasePo.getTaskId(), "auditorName", sysUser.getName());
        // 3. 完成任务并传递变量（流程会自动流转到下一节点）
        taskService.complete(flowableCommentBasePo.getTaskId(), variables,null);
        return true;
    }

    @Override
    public Boolean companyReTry(FlowableCommentBasePo flowableCommentBasePo) {
        SysUser sysUser = SecurityUtils.getLoginUser();
        Map<String, Object> commited = flowableCommentBasePo.getVariables();
        // 1. 验证提交参数是否完整
        Assert.notNull( commited.get("commitType"), "审核结果不能为空");
        Integer commitType  = (Integer) flowableCommentBasePo.getVariables().get("commitType");
        // 1. 构建审核结果变量
        Map<String, Object> variables = new HashMap<>();
        variables.put("commitType ", commitType); // 操作选择(放弃:0,重新提交:1)
        if(commited.containsKey("auditComment"))
            variables.put("auditComment", commited.get("auditComment")); // 更改说明
        // 3. 完成任务并传递变量（流程会自动流转到下一节点）
        taskService.complete(flowableCommentBasePo.getTaskId(), variables,null);
        return true;
    }


    @Override
    public Boolean selectAuditType(FlowableCommentBasePo flowableCommentBasePo) {
        SysUser sysUser = SecurityUtils.getLoginUser();
        Map<String, Object> commited = flowableCommentBasePo.getVariables();
        // 1. 验证提交参数是否完整
        Assert.notNull( commited.get("approvalFlowType"), "审批流程类型不能为空");
        Integer approvalFlowType  = (Integer) flowableCommentBasePo.getVariables().get("approvalFlowType");
        // 1. 构建审核结果变量
        Map<String, Object> variables = new HashMap<>();
        variables.put("approvalFlowType ", approvalFlowType); // 审批流程类型
        if(commited.containsKey("auditComment"))
            variables.put("auditComment", commited.get("auditComment")); // 审核意见
        //记录审核人信息
        taskService.setVariableLocal(flowableCommentBasePo.getTaskId(), "auditor", sysUser.getId());
        taskService.setVariableLocal(flowableCommentBasePo.getTaskId(), "auditorName", sysUser.getName());
        // 3. 完成任务并传递变量（流程会自动流转到下一节点）
        taskService.complete(flowableCommentBasePo.getTaskId(), variables,null);
        return true;
    }
}
